package com.example.gulimall.ware.service.impl;

import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.ware.dao.PurchaseDetailDao;
import com.example.gulimall.ware.entity.PurchaseDetailEntity;
import com.example.gulimall.ware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageCondition(Map<String, Object> params) {

        QueryWrapper<PurchaseDetailEntity> wrapper=new QueryWrapper<PurchaseDetailEntity>();
        if (params.containsKey("key")){
            String key=params.get("key").toString();
            if (!StringUtils.isEmpty(key)){
                wrapper.and(w-> {
                w.eq("purchase_id", key)
                .or().eq("sku_id", key)
                .or().eq("sku_num", key)
                .or().eq("sku_price", key);
                });
            }
        }
        if (params.containsKey("status")){
            String status=params.get("status").toString();
            if (!StringUtils.isEmpty(status)){
                wrapper.eq("status",status);
            }
        }

        if (params.containsKey("wareId")){
            String wareId=params.get("wareId").toString();
            if (!StringUtils.isEmpty(wareId)){
                wrapper.eq("ware_id",wareId);
            }
        }
        IPage<PurchaseDetailEntity> page=this.page(
               new  Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );
        return  new PageUtils(page);
    }

}